package com.tledu.stu_manager.controller.student; /**
 *
 */

import com.alibaba.fastjson.JSONObject;
import com.tledu.stu_manager.constant.Constant;
import com.tledu.stu_manager.dao.StudentDao;
import com.tledu.stu_manager.exception.CustomException;
import com.tledu.stu_manager.result.Result;
import com.tledu.stu_manager.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "StuBatchDelServlet", value = "/stu/batch_del")
public class StuBatchDelServlet extends HttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        super.init();
        studentService = new StudentService(new StudentDao());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(Constant.CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        String idList = request.getParameter("idList");
        try {
            studentService.batchDelStudent(idList);
            out.print(JSONObject.toJSONString(new Result(200, true, "删除成功")));
        } catch (CustomException e) {
            e.printStackTrace();
            out.print(JSONObject.toJSONString(new Result(500, false, e.getMessage())));
        }

    }
}
